package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.TaskPersonRelationLogic;
import cn.topevery.garden.entity.dto.TaskPersonRelationDTO;
import cn.topevery.garden.entity.po.TaskPersonRelation;
import cn.topevery.garden.entity.query.TaskPersonRelationQuery;
import cn.topevery.garden.entity.vo.TaskPersonRelationVO;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 人员任务关系表
 *
 * @author code-generate
 * @date 2021-09-02
 */
@Api(tags = "人员任务关系表")
@RestController
@RequestMapping("/taskPersonRelation")
public class TaskPersonRelationController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    TaskPersonRelationLogic taskPersonRelationLogic;


    /**
     * 获取人员任务关系表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取人员任务关系表页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<TaskPersonRelationVO>> listPage(@RequestBody TaskPersonRelationQuery query) {
        return JsonResult.ok(taskPersonRelationLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 人员任务关系表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody TaskPersonRelationDTO dto) {
        taskPersonRelationLogic.saveOrUpdate(getUser().id.toString(), mapper.map(dto, TaskPersonRelation.class));
        return JsonResult.ok(true);
    }

    @GetMapping(value = "/getByTaskId")
    @ApiOperation(value = "通过任务id获取关系")
    public JsonResult<?> getByTaskId(Integer taskId) {
        return JsonResult.ok(taskPersonRelationLogic.getByTaskId(taskId));
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        taskPersonRelationLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出人员任务关系表信息")
    @GetMapping("/export")
    public void export(TaskPersonRelationQuery query, HttpServletResponse response) {
        List<TaskPersonRelationVO> list = taskPersonRelationLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "人员任务关系表", "sheet1", TaskPersonRelationVO.class, "人员任务关系表", response);
    }

}
